<% layout('/layouts/default.html', {title: '服务订单管理', libs: ['validate','fileupload','dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(serviceOrder.isNewRecord ? '新增服务订单' : '编辑服务订单')}
			</div>
			<div class="box-tools pull-right">
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${serviceOrder}" action="${ctx}/order/serviceorder/serviceOrder/save" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<#form:hidden path="serviceOrderId"/>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('编号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="serviceOrderCode" maxlength="100" class="form-control" placeholder="自动生成" readonly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('国内资方')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="investorId" items="${gnzfList}" itemLabel="supplierName" itemValue="supplierId"
								class="form-control required" onchange="investorChange()" blankOption="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('代理公司')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:treeselect id="company" title="${text('公司选择')}"
								path="company.companyCode" labelPath="company.companyName"
								url="${ctx}/sys/company/treeData" canSelectRoot="true" canSelectParent="true"
								class=" required" allowClear="true" callbackFuncName="companyCallback"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('年度汽车买卖协议')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="yearCarSalePurchaseAgreementId" maxlength="64" class="form-control required" readonly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('客户')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="customerId" items="${customerList}" onchange="customerChange()" maxlength="64"
                                itemLabel="customerName" itemValue="customerId"  blankOption="true" class="form-control" />

								<!--<#form:treeselect id="customerId" title="${text('客户选择')}"
								path="customer.customerId" labelPath="customer.customerName"
								url="${ctx}/base/customer/customer/treeData" canSelectRoot="true" canSelectParent="true"
								class=" required" allowClear="true" callbackFuncName="customerCallback"/>-->
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('汽车买卖合同号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="contractNumber" maxlength="50" class="form-control " placeholder="自动生成" readonly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('供应链服务协议')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="supplyChainServiceAgreementId" maxlength="64" class="form-control required" readonly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('外商')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="foreignBusinessmenId" items="${hwzfList}" itemLabel="supplierName" itemValue="supplierId"
								class="form-control required" onchange="foreignBusinessmenChange()" blankOption="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('采购委托书号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="purchaseCommissionNumber" maxlength="100" class="form-control " placeholder="自动生成" readonly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('供货外商')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="supplierId" items="${ghwsList}" itemLabel="supplierName" itemValue="supplierId"
								class="form-control required" blankOption="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('外贸合同号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="foreignTradeContractNumber" maxlength="100" class="form-control required"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('付款方式')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="paymentMethod" dictType="payment_method" class="form-control required"
								blankOption="true" onchange="paymentMethodChange()"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('订单日期')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="date" readonly="true" maxlength="20" class="form-control laydate required"
									dataFormat="date" data-type="date" data-format="yyyy-MM-dd"/>
							</div>
						</div>
					</div>
				</div>
				<div class="form-unit">${text('状态')}</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('状态')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="orderStatus" dictType="order_status" class="form-control" readonly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('审批状态')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="approveStatus" dictType="finance_bill_status" class="form-control" readonly="true"/>
							</div>
						</div>
					</div>
				</div>
				<div class="form-unit">${text('交易信息')}</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('最晚装船日期')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="latestShipmentDate" readonly="true" maxlength="20" class="form-control laydate required"
									dataFormat="date" data-type="date" data-format="yyyy-MM-dd"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('装运港')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="fromPort" dictType="erp_harbor_list" class="form-control required" blankOption="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('目的港')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="toPort" dictType="erp_harbor_list" class="form-control required" blankOption="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('贸易术语')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="tradeTerm" dictType="trade_term" class="form-control required" blankOption="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('币种')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="currency" dictType="sys_bizhong" class="form-control required" blankOption="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('资方利润')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="investorProfit" class="form-control required number"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('批单号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="lotNumber" maxlength="100" class="form-control" readonly="true"/>
							</div>
						</div>
					</div>
				</div>
				<div class="form-unit">${text('车辆信息&风险保证金')}</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('保证金、费用比例')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="marginExpenseRatioId" itemLabel="name" itemValue="id"
								class="form-control required" blankOption="true" onchange="marginExpenseRatioIdChange()"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('保证金比例')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="marginRatio" maxlength="10" class="form-control" readonly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('押汇比例')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="negotiatedRatioId" maxlength="64" class="form-control" readonly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('预收费比例')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="prePayRatio" maxlength="10" class="form-control" readonly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('暂定汇率')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="tentativeExchangeRate" maxlength="10" class="form-control required"/>
							</div>
						</div>
					</div>
				</div>
				<h4 class="form-unit">${text('服务订单明细')}</h4>
				<div class="ml10 mr10">
					<table id="serviceOrderDetailDataGrid"></table>
					<% if (hasPermi('order:serviceorder:serviceOrder:edit')){ %>
					<a href="#" id="serviceOrderDetailDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> ${text('增行')}</a>
					<a href="#" id="serviceOrderDetailDataGridAddRowBtn1" style="display:none" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> ${text('增行')}</a>
					<% } %>
				</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2" title="">
								<span class="required hide">*</span> ${text('车辆主要配置/备注')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-10">
								<#form:textarea path="remarks" rows="4" maxlength="500" class="form-control"/>
							</div>
						</div>
					</div>
				</div>
				<div class="form-unit">${text('附件上传')}</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2">
								<span class="required hide">*</span> ${text('附件上传')}：</label>
							<div class="col-sm-10">
								<#form:fileupload id="uploadFile" bizKey="${serviceOrder.id}" bizType="serviceOrder_file"
								uploadType="all" class="" readonly="false" preview="true"/>
							</div>
						</div>
					</div>
				</div>
				<div class="form-unit">${text('版本信息')}</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('版本号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="version" class="form-control digits" readonly="true"/>
							</div>
						</div>
					</div>
				</div>
				<div class="form-unit">${text('系统信息')}</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('创建者')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="createBy" maxlength="64" class="form-control " readonly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('创建时间')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="createDate" readonly="true" maxlength="20" class="form-control "
								dataFormat="date" data-type="date" data-format="yyyy-MM-dd"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('更新者')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="updateBy" maxlength="64" class="form-control " readonly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('更新时间')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="updateDate" class="form-control " readonly="true"
								dataFormat="date" data-type="date" data-format="yyyy-MM-dd"/>
							</div>
						</div>
					</div>
				</div>
				<div class="form-unit">${text('相关记录')}</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('信用证')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="letterCreditCode" items="${listLetterCredit}"
								itemLabel="letterCreditCode" itemValue="letterCreditId" blankOption="true" class="form-control required" readonly="true"/>
							</div>
						</div>
					</div>
				</div>

			</div>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
						<% if (hasPermi('order:serviceorder:serviceOrder:edit')){ %>
							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;
						<% } %>
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<% } %>
<script>
//初始化服务订单明细DataGrid对象
$("#serviceOrderDetailDataGrid").dataGrid({
	data: ${toJson(serviceOrder.serviceOrderDetailList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度
	
	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'serviceOrderDetailId', editable:true, hidden:true},
		{header:'${text("车辆")}', name:'carId', width:300,
			editable:true, edittype:'select', editoptions:{'class':'form-control required',
				items: ${toJson(tcarList)},
				itemLabel: 'carName', itemValue: 'carId', dataInit: function(element){
					$(element).select2().on("change",function(){
						let car =  {};
						$(this).resetValid();
						$.ajax({
							type: "POST",
							async : false,
							url: "${ctx}/order/serviceorder/serviceOrder/getCarInfo",
							data: {"carId": $(this).val()},
							success: function (data) {
								car.area = data.area;
								car.brand = data.brand;
								car.model = data.model;
							}
						});
						$(this).closest('tr').find('[aria-describedby="serviceOrderDetailDataGrid_brand"] .form-control').val(car.brand).trigger('change');
						$(this).closest('tr').find('[aria-describedby="serviceOrderDetailDataGrid_area"] .form-control').val(car.area).trigger('change');
						$(this).closest('tr').find('[aria-describedby="serviceOrderDetailDataGrid_model"] .form-control').val(car.model);
					});
				}
			}
		},
		{header:'${text("车架号")}', name:'frameNumber', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'100', 'class':'form-control', 'readonly':'true'}},
		{header:'${text("品牌")}', name:'brand', width:200,
			editable:true, edittype:'select', editoptions:{'class':'form-control',  'readonly':'true',
				items: $.merge([{dictLabel:'&nbsp;',dictValue:''}], ${@DictUtils.getDictListJson('car_pinpai')}),
				itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid()});
				}
			}
		},
		{header:'${text("版型")}', name:'area', width:200, editable:true, edittype:'select', editoptions:{'class':'form-control', 'readonly':'true',
				items: $.merge([{dictLabel:'&nbsp;',dictValue:''}], ${@DictUtils.getDictListJson('car_quyu')}),
				itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid()});
				}
			}
		},
		{header:'${text("车型")}', name:'model', width:200, editable:true, edittype:'text', editoptions:{'maxlength':'100', 'class':'form-control', 'readonly':'true'}},
		{header:'${text("排量")}', name:'displacement', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'50', 'class':'form-control required'}},
		{header:'${text("能源类型")}', name:'energyType', width:200,
			editable:true, edittype:'select', editoptions:{'class':'form-control required',
				items: $.merge([{dictLabel:'&nbsp;',dictValue:''}], ${@DictUtils.getDictListJson('car_nengyuanleixing')}),
				itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid()});
				}
			}
		},
		{header:'${text("海关税率")}', name:'customsTariffRate', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'10', 'class':'form-control required'}},
		{header:'${text("数量")}', name:'quantity', width:100, editable:true, edittype:'text', editoptions:{'class':'form-control digits', 'readonly':'true'}},
		{header:'${text("单价")}', name:'unitPrice', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control required number',
				dataInit: function(element){
					$(element).change(function(){
						var $select = $(this);
						$(this).resetValid();
						let quantity = $(this).closest('tr').find('[aria-describedby="serviceOrderDetailDataGrid_quantity"] .form-control').val();
						let total = quantity * $(this).val();
						$(this).closest('tr').find('[aria-describedby="serviceOrderDetailDataGrid_amount"] .form-control').val(total);
						let riskMargin = $("#marginRatio").val()*total/100*$("#tentativeExchangeRate").val();
						$(this).closest('tr').find('[aria-describedby="serviceOrderDetailDataGrid_riskMargin"] .form-control').val(riskMargin);
						$(this).closest('tr').find('[aria-describedby="serviceOrderDetailDataGrid_riskMargin1"] .form-control').val(riskMargin);
						$.ajax({
							type: "POST",
							url: "${ctx}/order/serviceorder/serviceOrder/getAgencyFee",
							data: {"investorId": $("#investorId").val(),
									"amount": total * $("#tentativeExchangeRate").val(),
									},
							success: function (data) {
								$select.closest('tr').find('[aria-describedby="serviceOrderDetailDataGrid_agencyFee"] .form-control').val(data.salesPrice);
								$select.closest('tr').find('[aria-describedby="serviceOrderDetailDataGrid_agencyFee1"] .form-control').val(data.purchasePrice);
							}
						});

						let prePay = $("#prePayRatio").val()*total/100*$("#tentativeExchangeRate").val();
						$(this).closest('tr').find('[aria-describedby="serviceOrderDetailDataGrid_prePay"] .form-control').val(prePay);
						let agencyFee = $(this).closest('tr').find('[aria-describedby="serviceOrderDetailDataGrid_agencyFee"] .form-control').val();
						let totalCost = Number(agencyFee) + Number(riskMargin) + Number(prePay);
						$(this).closest('tr').find('[aria-describedby="serviceOrderDetailDataGrid_totalCost"] .form-control').val(totalCost);
					});
				}
		}},
		{header:'${text("车辆总价")}', name:'amount', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control required number'}},
		{header:'${text("客户代理费（RMB）")}', name:'agencyFee', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number', 'readonly':'true'}},
		{header:'${text("客户风险保证金（RMB）")}', name:'riskMargin', width:200, editable:true, edittype:'text', editoptions:{'class':'form-control number', 'readonly':'true'}},
		{header:'${text("客户预收费用（RMB）")}', name:'prePay', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number', 'readonly':'true'}},
		{header:'${text("客户费用合计（RMB）")}', name:'totalCost', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number', 'readonly':'true'}},
		{header:'${text("交货地点")}', name:'tradingLocation', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'50', 'class':'form-control'}},
		{header:'${text("资方代理费（RMB）")}', name:'agencyFee1', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number', 'readonly':'true'}},
		{header:'${text("资方风险保证金（RMB）")}', name:'riskMargin1', width:200, editable:true, edittype:'text', editoptions:{'class':'form-control number', 'readonly':'true'}},
		{header:'${text("服务订单ID")}', name:'serviceOrderId.serviceOrderId', editable:true, hidden:true},
		{header:'${text("操作")}', name:'actions', width:80, sortable:false, fixed:true, formatter: function(val, obj, row, act){
			var actions = [];
			if (val == 'new'){
				actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#serviceOrderDetailDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}else{
				actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#serviceOrderDetailDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}
				actions.push('<a href="#" onclick="copyCurrentRow(\''+obj.rowId+'\')"><i class="fa fa-copy"></i></a>&nbsp;');
			return actions.join('');
		}, editoptions: {defaultValue: 'new'}}
	],

    frozenCols:true,
	// 编辑表格参数
	editGrid: true,				// 是否是编辑表格
	editGridInitRowNum: 0,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#serviceOrderDetailDataGridAddRowBtn1'),	// 子表增行按钮
	editGridAddRowInitData: {serviceOrderDetailId: '', status: Global.STATUS_NORMAL, quantity: 1, agencyFee: 0, riskMargin: 0, prePay: 0,agencyFee1: 0, riskMargin1: 0},	// 新增行的时候初始化的数据
	
	// 编辑表格的提交数据参数
	editGridInputFormListName: 'serviceOrderDetailList', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,serviceOrderDetailId,rowNumber,carId,frameNumber,brand,area,model,displacement,energyType,customsTariffRate,quantity,unitPrice,amount,agencyFee,riskMargin,prePay,totalCost,tradingLocation,logisticsStatus,lockFlag,serviceOrderId.serviceOrderId,agencyFee1,riskMargin1', // 提交数据列表的属性字段
	
	// 加载成功后执行事件
	ajaxSuccess: function(data){
	}
});
</script>
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($(form), function(data){
			js.showMessage(data.message);
			if(data.result == Global.TRUE){
				js.closeCurrentTabPage(function(contentWindow){
					contentWindow.page();
				});
			}
		}, "json");
    }
});

if($("#serviceOrderId").val() != ""){
	paymentMethodChange();
}
$('#serviceOrderDetailDataGridAddRowBtn').click(function(){
let tentativeExchangeRate=$('#tentativeExchangeRate').val();
	if(!tentativeExchangeRate){
		js.showErrorMessage("请先填写暂定汇率！");
	}
	else{
		$('#serviceOrderDetailDataGridAddRowBtn1').click();
	}


});
function foreignBusinessmenChange() {
	if ($("#foreignBusinessmenId").val() != "") {
		$("#supplierId").val($("#foreignBusinessmenId").val()).trigger('change');
	} else {
		$("#supplierId").val("").trigger('change');
	}
}
function customerChange() {
	let customerId = $("#customerId").val();
	if (customerId) {
		$.ajax({
			type: "POST",
			url: "${ctx}/order/serviceorder/serviceOrder/getSupplyChainServiceAgreement",
			data: {"customerId": customerId},
			success: function(data){
				$("#supplyChainServiceAgreementId").attr("value",data);
				if(data == ""){
					js.showErrorMessage("该客户没有供应链服务协议！");
				}
			}
		});
	} else {
		$("#supplyChainServiceAgreementId").attr("value","");
	}
}
function customerCallback(id, act, index, layero, nodes){
	if (id == 'customer'){
		if(act == 'ok'){
			$.ajax({
				type: "POST",
				url: "${ctx}/order/serviceorder/serviceOrder/getSupplyChainServiceAgreement",
				data: {"customerId": nodes[0].id},
				success: function(data){
					$("#supplyChainServiceAgreementId").attr("value",data);
					if(data == ""){
						js.showErrorMessage("该客户没有供应链服务协议！");
					}
				}
			});
		}
		if(act == 'clear'){
			$("#supplyChainServiceAgreementId").attr("value","");
		}
	}
}

function investorChange() {
	if ($("#investorId").val() != "") {
		$.ajax({
			type: "POST",
			url: "${ctx}/order/serviceorder/serviceOrder/getYearCarSalePurchaseAgreement",
			data: {"supplierId": $("#investorId").val()},
			success: function (data) {
				$("#yearCarSalePurchaseAgreementId").attr("value", data);
				if(data == ""){
					js.showErrorMessage("该国内资方没有年度汽车买卖协议！");
				}
			}
		});
	} else {
		$("#yearCarSalePurchaseAgreementId").attr("value","");
	}
}

function paymentMethodChange(){
		$.ajax({
			type : "post",
			async : false,
			url : "${ctx}/order/serviceorder/serviceOrder/getList",
			data : {
				'supplierId' : $("#investorId").val(),
				'paymentMethod' : $("#paymentMethod").val()
			},
			dataType : "json",
			success : function(msg) {
				$("#marginExpenseRatioId").val("");
				$("#marginExpenseRatioId").empty();
				if (msg.length > 0) {
					for (var i = 0; i < msg.length; i++) {
						var partId = msg[i].id;
						var partName = msg[i].name;
						var option = $("<option>").attr({
							"value" : partId
						}).text(partName);
						$("#marginExpenseRatioId").append(option);
					}
					$("#marginExpenseRatioId option:first").prop("selected", 'selected');
					$("#marginExpenseRatioId").change();
				}
			},
			error : function(json) {
				js.showErrorMessage("网络异常！");
			}
		});
}

function marginExpenseRatioIdChange(){
	$("#marginRatio").val("");
	$("#negotiatedRatioId").val("");
	$("#prePayRatio").val("");
	if ($("#marginExpenseRatioId").val() != "") {
		$.ajax({
			type: "POST",
			url: "${ctx}/order/serviceorder/serviceOrder/getRatioInfo",
			data: {
					"id": $("#marginExpenseRatioId").val(),
					'paymentMethod' : $("#paymentMethod").val()
			},
			success: function (data) {
				$("#marginRatio").val(data.marginRatio);
				$("#negotiatedRatioId").val(data.negotiatedRatioId);
				$("#prePayRatio").val(data.prePayRatio);
			}
		});
	}
}

// 复制一行数据
function copyCurrentRow(rowID) {
	$('#serviceOrderDetailDataGrid').dataGrid('saveRow', rowID, true);
	$("#serviceOrderDetailDataGrid").dataGrid("restoreRow", rowID);
	var rowData = $("#serviceOrderDetailDataGrid").dataGrid("getRowData", rowID);
	// log("rowData=" + JSON.stringify(rowData));
	$("#serviceOrderDetailDataGrid").jqGrid("addRow", {
		position: "last",
		addRowParams: {keys: false, focusField: true},
		initdata: {id: "", status: Global.STATUS_NORMAL,serviceOrderDetailId: '', carId: rowData.carId,frameNumber:rowData.frameNumber,
			brand:rowData.brand,area:rowData.area,model:rowData.model,displacement:rowData.displacement,energyType:rowData.energyType,
			customsTariffRate:rowData.customsTariffRate,quantity:rowData.quantity,unitPrice:rowData.unitPrice,amount:rowData.amount,
			agencyFee:rowData.agencyFee,riskMargin:rowData.riskMargin,prePay:rowData.prePay,totalCost:rowData.totalCost,tradingLocation:rowData.tradingLocation,
			agencyFee1:rowData.agencyFee1,riskMargin1:rowData.riskMargin1
		}
	});
	$("#serviceOrderDetailDataGrid").dataGrid("editRow", rowID, {keys: true, focusField: true});

    if (typeof window.webuploaderRefresh == 'function'){
        window.webuploaderRefresh();
    }
}
</script>